package com.aica.handler;

import com.aica.constant.ResultConstant;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登录失败的处理类
 * @author by
 */
@Component
public class LoginFailureHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException {

        httpServletResponse.setStatus(HttpStatus.OK.value());
        httpServletResponse.setContentType("text/json;charset=utf-8");

        httpServletResponse.getWriter().write(new ObjectMapper().writeValueAsString(ResultConstant.CODE_ERROR));
    }

}
